﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using Empresa.Sistema.Datos.Entidades;

namespace Empresa.Sistema.Datos
{
    public class Editoriales
    {

        private string ConnectionString = "Data Source=.;Initial Catalog=SistemaBibliotecaV1;Integrated Security=True";
        public void Adicionar(Editorial edi)
        {
            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_InsertarEditorial";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Codigo", edi.Codigo));
                        cmd.Parameters.Add(new SqlParameter("@Nombre", edi.Nombre));
                        cmd.Parameters.Add(new SqlParameter("@Idioma", edi.Telefono));
                        cmd.Parameters.Add(new SqlParameter("@Cantidad", edi.Direccion));
                        cmd.Parameters.Add(new SqlParameter("@Estado", edi.Mail));                       
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException ex)
            {

            }

        }

        public void Modificar(Editorial edi)
        {
            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_ModificarEditorial";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Id", edi.Id));
                        cmd.Parameters.Add(new SqlParameter("@Codigo", edi.Codigo));
                        cmd.Parameters.Add(new SqlParameter("@Nombre", edi.Nombre));
                        cmd.Parameters.Add(new SqlParameter("@Idioma", edi.Telefono));
                        cmd.Parameters.Add(new SqlParameter("@Cantidad", edi.Direccion));
                        cmd.Parameters.Add(new SqlParameter("@Estado", edi.Mail)); 
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException ex)
            {

            }

        }

        public void Eliminar(Editorial edi)
        {

            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_EliminarEditorial";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Id", edi.Id));
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException ex)
            {

            }

        }

        public List<Editorial> Listar()
        {
            List<Editorial> lista = new List<Editorial>();

            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_ListarEditorial";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        SqlDataReader rdr = cmd.ExecuteReader();
                        while (rdr.Read())
                        {
                            var edi = new Editorial();
                            edi.Id = rdr.GetInt32(rdr.GetOrdinal("Id"));
                            edi.Codigo = rdr.GetString(rdr.GetOrdinal("Codigo"));
                            edi.Nombre = rdr.GetString(rdr.GetOrdinal("Nombre"));
                            edi.Telefono = rdr.GetString(rdr.GetOrdinal("Telefono"));
                            edi.Direccion = rdr.GetString(rdr.GetOrdinal("Direccion"));
                            edi.Mail = rdr.GetString(rdr.GetOrdinal("Mail"));
                            lista.Add(edi);
                        }
                    }

                }
            }
            catch (SqlException ex)
            {

            }
            return lista;
        }

        public List<Editorial> ConsultaPorNombre(string Nombre)
        {
            List<Editorial> lista = new List<Editorial>();

            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_ConsultaEditorialPorNombre";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Nombre", Nombre));
                        SqlDataReader rdr = cmd.ExecuteReader();
                        while (rdr.Read())
                        {
                            var edi = new Editorial();
                            edi.Id = rdr.GetInt32(rdr.GetOrdinal("Id"));
                            edi.Codigo = rdr.GetString(rdr.GetOrdinal("Codigo"));
                            edi.Nombre = rdr.GetString(rdr.GetOrdinal("Nombre"));
                            edi.Telefono = rdr.GetString(rdr.GetOrdinal("Telefono"));
                            edi.Direccion = rdr.GetString(rdr.GetOrdinal("Direccion"));
                            edi.Mail = rdr.GetString(rdr.GetOrdinal("Mail"));
                            lista.Add(edi);
                        }
                    }
                }
            }
            catch (SqlException ex)
            {

            }
            return lista;
        }


    }
}
